//
//  KXDetailImgCell.swift
//  KXBook
//
//  Created by liujianlong on 2022/9/16.
//

import UIKit

class KXDetailImgCell: UICollectionViewCell {
    @IBOutlet var collectionView: UICollectionView!
    override func awakeFromNib() {
        super.awakeFromNib()
        self.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
    }
    var model:KXRecord=KXRecord(){
        didSet{
            self.collectionView.reloadData()
        }
    }
}


//MARK: - UICollectionViewDataSource
extension KXDetailImgCell : UICollectionViewDataSource
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.model.datas.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"cell", for: indexPath)
        let data = self.model.datas[indexPath.row]
        let img = UIImageView()
        img.image = UIImage(data:data as! Data)
        img.translatesAutoresizingMaskIntoConstraints = false
        cell.contentView.addSubview(img)
        cell.contentView.backgroundColor = .white
        cell.cornerRadius = 10
        let top = NSLayoutConstraint(item:img, attribute: .top, relatedBy: .equal, toItem:cell.contentView, attribute: .top, multiplier: 1, constant: 0)
        let left = NSLayoutConstraint(item:img, attribute: .left, relatedBy: .equal, toItem:cell.contentView, attribute: .left, multiplier: 1, constant: 0)
        let bottom = NSLayoutConstraint(item:img, attribute: .bottom, relatedBy: .equal, toItem:cell.contentView, attribute: .bottom, multiplier: 1, constant: 0)
        let right = NSLayoutConstraint(item:img, attribute: .right, relatedBy: .equal, toItem:cell.contentView, attribute: .right, multiplier: 1, constant: 0)
        cell.contentView.addConstraints([top,left,bottom,right])
        return cell
    }
}
//MARK: - UICollectionViewDelegate
extension KXDetailImgCell : UICollectionViewDelegate
{
}

//MARK: - UICollectionViewDelegateFlowLayout
extension KXDetailImgCell : UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        let w = (kUIScreenWidth - 20 * 4) / 1
        return CGSize(width: w, height: w)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
    {
        return 20
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
    {
        return 20
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
    {
        return UIEdgeInsets(top:20, left: 20, bottom: 20, right: 20)
    }
}
